<extend name="Layout/application"/>

<block name="css">
   <style>
      .area {
         width: 280px;
      }

      .area li {
         float: left;
      }

      .dropdown-toggle {
         padding: 0;
      }
   </style>
</block>

<block name="content">
   <section class="content-header">
      <h1>
         客资订单
         <small>Order index</small>
      </h1>
      <ol class="breadcrumb">
         <li><a href="#"><i class="fa fa-dashboard"></i> Order</a></li>
         <li class="active">Index</li>
      </ol>
   </section>

   <section class="content">

      <include file="Layout/_flash"/>

      <div class="row">
         <div class="col-md-12">
            <div class="box">
               <div class="box-header">
                  <div class="col-md-5">
                     <a href="javascript:;" class="btn btn-primary btn-refresh" title="刷新"><i
                          class="fa fa-refresh"></i></a>
                     <a href="javascript:;" class="btn btn-danger btn-del delete_all" title="删除"><i
                          class="fa fa-trash"></i> 删除</a>
                     <a href="/admin/orders/export_order" class="btn btn-primary btn-add" title="导出"><i
                          class="fa fa-sign-in"></i>
                        导出</a>
                  </div>

                  <!--搜索-->
                  <form action="" method="get">
                     <!--日期选择-->
                     <div class="col-md-4">
                        <div class="input-group input-group-sm" style="width: 280px;margin-top: 10px;">
                           <div class="input-group-btn">
                              <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                 <span class="field">{{$fieldDate|default='订单时间'}}</span>
                                 <i class="fa fa-caret-down"></i></button>
                              <input type="hidden" class="search_field" name="search_field"
                                     value="order_time">
                              <ul class="dropdown-menu search-ul">
                                 <li><a href="javascript:;" data-field="order_time">订单时间</a></li>
                                 <li><a href="javascript:;" data-field="hz_time">跟妆日期</a></li>
                              </ul>
                           </div>
                           <input type="text" name="date" value="{{$Request.param.date}}"
                                  class="form-control date">
                           <div class="input-group-btn">
                              <button type="submit" class="btn btn-default"><i class="fa fa-search"></i>
                              </button>
                           </div>
                        </div>
                     </div>
                  </form>

                  <form action="" method="get">
                     <!--输入框搜索-->
                     <div class="col-md-3">
                        <div class="input-group input-group-sm" style="width:280px;margin-top: 10px;">
                           <div class="input-group-btn">
                              <button type="button" class="btn btn-default dropdown-toggle search-drop-btn"
                                      data-toggle="dropdown" aria-expanded="false">
                                 <span class="field">{{$fieldName|default='客资ID'}}</span>
                                 <i class="fa fa-caret-down"></i>
                              </button>
                              <input type="hidden" class="search_field" name="search_field"
                                     value="customer_id">
                              <ul class="dropdown-menu search-ul">
                                 <li><a href="javascript:void(0)" data-field="customer_id">客资ID</a></li>
                                 <li><a href="javascript:void(0)" data-field="name">姓名</a></li>
                                 <li><a href="javascript:void(0)" data-field="phone">电话</a></li>
                                 <li><a href="javascript:void(0)" data-field="wang">网销</a></li>
                                 <li><a href="javascript:void(0)" data-field="ordercode">订单编号</a></li>
                                 <li><a href="javascript:void(0)" data-field="taocan">套餐</a></li>
                                 <li><a href="javascript:void(0)" data-field="pay_type">支付方式</a></li>
                                 <li><a href="javascript:void(0)" data-field="hz_name">化妆师</a></li>
                              </ul>
                           </div>
                           <input type="text" name="title" class="form-control pull-right"
                                  value="{{$Request.param.title}}" placeholder=""/>
                           <div class="input-group-btn">
                              <button type="submit" class="btn btn-default"><i class="fa fa-search"></i>
                              </button>
                           </div>
                        </div>
                     </div>
                  </form>

               </div>

               <!-- /.box-header -->
               <div class="box-body">
                  <table id="example2" class="table table-bordered table-hover">
                     <thead>
                     <tr>
                        <th>
                           <input type="checkbox" class="check_all">
                        </th>
                        <th>客资ID <a href="" class="fa fa-sort"></a></th>
                        <th>姓名</th>
                        <th>电话</th>
                        <th>网销</th>
                        <th>订单编号</th>
                        <th>
                           <div class="btn-group">
                              <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                                 是否订单<span class="caret"></span>
                              </button>
                              <ul class="dropdown-menu">
                                 <li><a href="">全部</a></li>
                                 <volist name="isOrders" id="isOrder">
                                    <li><a href="/admin/orders/index?is_order={{$key}}">{{$isOrder}}</a></li>
                                 </volist>
                              </ul>
                           </div>
                        </th>
                        <th>套餐</th>
                        <th>订单说明</th>
                        <th>订单时间</th>
                        <th>总金额</th>
                        <th>支付方式</th>
                        <th>实付金额</th>
                        <th>余款</th>
                        <th>其他金额</th>
                        <th>化妆师</th>
                        <th>跟妆日期</th>
                        <th>备注</th>
                     </tr>
                     </thead>
                     <tbody>
                     <volist name="orders" id="order">
                        <tr data-id="{{$order.id}}">
                           <td>
                              <input type="checkbox" class="checked_id" name="check_id[]" value="{{$order.id}}">
                           </td>
                           <td>
                              <button type="button" class="log" data-toggle="modal" data-target="#log" title="日志"
                                      data-id="{{$order.customer.id}}">{{$order.customer.id}}
                              </button>
                           </td>
                           <td>{{$order.customer.name}}</td>
                           <td>{{$order.customer.phone}}</td>
                           <td>{{$order.customer.wang}}</td>
                           <td>
                              <input type="text" class="phone edit" name="ordercode" value="{{$order.ordercode}}">
                           </td>
                           <!--是否订单-->
                           <td>
                              {{:is_something($order,'is_order')}}
                           </td>
                           <!--套餐-->
                           <td>
                              <div class="btn-group">
                                 <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                                    {{$order.taocan|default='未选择'}}<span class="caret"></span>
                                 </button>
                                 <ul class="dropdown-menu">
                                    <volist name="taocans" id="taocan">
                                       <li><a href="javascript:;" class="update" data-name="taocan"
                                              data-value="{{$taocan}}">{{$taocan}}</a></li>
                                    </volist>
                                 </ul>
                              </div>
                           </td>
                           <!--订单说明-->
                           <td>
                              <button data-toggle="modal" data-target="#beizhu1" class="beizhu" title="订单说明"
                                      data-value="{{$order.order_mark}}">
                                 <if $order.order_mark>
                                    <i class="fa fa-eye"></i>
                                    <else/>
                                    <i class="fa fa-eye-slash"></i>
                                 </if>
                              </button>
                           </td>
                           <!--订单时间-->
                           <td>
                              <input type="text" class="time order_time" data-id="{{$order.id}}" name="order_time"
                                     title="{{$order.order_time}}" value="{{$order.order_time}}">
                           </td>
                           <!--总金额-->
                           <td>
                              <input type="text" class="name edit" name="zje" value="{{$order.zje}}">
                           </td>
                           <!--支付方式-->
                           <td>
                              <div class="btn-group">
                                 <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                                    {{$order.pay_type|default='未选择'}}<span class="caret"></span>
                                 </button>
                                 <ul class="dropdown-menu">
                                    <volist name="payTypes" id="payType">
                                       <li><a href="javascript:;" class="update" data-name="pay_type"
                                              data-value="{{$payType}}">{{$payType}}</a></li>
                                    </volist>
                                 </ul>
                              </div>
                           </td>
                           <!--实付金额-->
                           <td>
                              <input type="text" class="name edit" name="sfje" value="{{$order.sfje}}">
                           </td>
                           <!--余款-->
                           <td>
                              <input type="text" class="name edit" name="yk" value="{{$order.yk}}">
                           </td>
                           <!--其他金额-->
                           <td>
                              <input type="text" class="name edit" name="qtje" value="{{$order.qtje}}">
                           </td>
                           <!--化妆师-->
                           <td>
                              <div class="btn-group">
                                 <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                                    {{$order.hz_name|default='未选择'}}<span class="caret"></span>
                                 </button>
                                 <ul class="dropdown-menu">
                                    <volist name="huazhuangs" id="huazhuang">
                                       <li><a href="javascript:;" class="update" data-name="hz_name"
                                              data-value="{{$huazhuang}}">{{$huazhuang}}</a></li>
                                    </volist>
                                 </ul>
                              </div>
                           </td>
                           <!--跟妆日期-->
                           <td>
                              <input type="text" class="time hz_time" data-id="{{$order.id}}" name="hz_time"
                                     title="{{$order.hz_time}}" value="{{$order.hz_time}}">
                           </td>
                           <!--备注-->
                           <td>
                              <button data-toggle="modal" data-target="#beizhu2" class="beizhu"
                                      data-value="{{$order.mark}}">
                                 <if $order.mark>
                                    <i class="fa fa-eye"></i>
                                    <else/>
                                    <i class="fa fa-eye-slash"></i>
                                 </if>
                              </button>
                           </td>
                        </tr>
                     </volist>

                     </tbody>
                  </table>

                  <div class="box-footer">
                     共 {{$orders->total()}} 条数据
                     <div class="page">
                        {{$orders->render()|raw}}
                     </div>
                  </div>
               </div>
               <!-- /.box-body -->
            </div>
            <!-- /.box -->
         </div>
         <!-- /.col -->
      </div>
      <!-- /.row -->
   </section>

   <!--备注日志-->
   <div class="modal fade" id="log">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span></button>
               <h4 class="modal-title">日志</h4>
            </div>
            <div class="modal-body" id="showLog">

            </div>
            <div class="modal-footer">
               <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
            </div>
         </div>
      </div>
   </div>

   <!--订单备注-->
   <div class="modal fade" id="beizhu1">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span></button>
               <h4 class="modal-title">订单备注</h4>
            </div>
            <div class="modal-body">

               <input type="hidden" id="mark1">
               <textarea name="order_mark" id="order_mark" cols="15" rows="5" class="form-control"
                         placeholder="请输入订单备注"></textarea>
            </div>
            <div class="modal-footer">
               <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
               <button type="button" class="btn btn-primary detailSubmit1">提交</button>
            </div>
         </div>
      </div>
   </div>

   <!--备注查看-->
   <div class="modal fade" id="beizhu2">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span></button>
               <h4 class="modal-title">备注</h4>
            </div>
            <div class="modal-body">
               <input type="hidden" id="mark2">
               <textarea name="mark" id="mark" cols="15" rows="5" class="form-control"
                         placeholder="请输入备注"></textarea>
            </div>
            <div class="modal-footer">
               <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
               <button type="button" class="btn btn-primary detailSubmit2">提交</button>
            </div>
         </div>
      </div>
   </div>
</block>

<block name="js">
   <script>
      $(function () {
         //日期范围选择
         laydate.render({
            elem: '.date',
            range: '~',
         });
         // 搜索框
         $('body').on("click", '.search-ul li a', function (event) {
            var _this = $(this);
            var _field = _this.data('field');
            var _html = _this.html();
            var _box = _this.closest('.input-group-btn');
            _box.find('.search_field').val(_field);
            _box.next('input').attr('placeholder', _html);
            _box.find('.field').html(_html);
         });

         //刷新
         $('.btn-refresh').click(function () {
            window.location.reload();
         });

         //上面选中，下面也选中
         $('.check_all').click(function () {
            var checked = $(this).prop('checked');
            $('.checked_id').prop('checked', checked).parents('tr').toggleClass('selected');
         });
         //点击切换背景色
         $('.checked_id').click(function () {
            $(this).parents('tr').toggleClass('selected');
         });

         //多选删除
         $('.delete_all').click(function () {
            var check_length = $('.checked_id:checked').length;
            if (check_length == 0) {
               layer.msg('请你至少选中一条', {icon: 2, offset: 't'});
               return false;
            }

            layer.confirm('确认删除选中的 ' + check_length + ' 条吗?',
               {icon: 3, title: '提示', offset: 't'},
               function (index) {
                  layer.close(index);

                  $.ajax({
                     type: 'DELETE',
                     url: '/admin/orders/delete_all',
                     data: $('.checked_id:checked').serialize(),
                     success: function (data) {
                        //判断有没有权限
                        if (data && data.status == 0) {
                           layer.msg(data.msg, {
                              icon: 5,
                              time: 2000, //2秒关闭（如果不配置，默认是3秒）
                              offset: 't'
                           });
                           return false;
                        } else {
                           $('.checked_id:checked').parents('tr').fadeOut(400);
                        }
                     }
                  });
               });
            return false;
         });

         //查看日志
         $('.log').click(function () {
            var log_id = $(this).data('id');
            $.ajax({
               type: 'POST',
               url: "{{:url('orders/showLog')}}",
               data: {log_id: log_id},
               success: function (data) {
                  //判断有没有权限
                  if (data && data.status == 0) {
                     layer.msg(data.msg, {
                        icon: 5,
                        time: 2000, //2秒关闭（如果不配置，默认是3秒）
                        offset: 't'
                     });
                     return false;
                  } else {
                     var str = '';
                     $.each(data, function (i, item) {
                        str += '<p>' + '[' + item.create_at + ']' + item.log + '</p>';
                     });
                     $('#showLog').html(str);
                     return false;
                  }
               }
            });
         });

         //改变属性---是否订单
         $('.change_attr').click(function () {
            var id = $(this).parents('tr').data('id');
            var attr = $(this).data('attr');
            var _this = $(this);

            layer.confirm('确认改变吗?',
               {icon: 3, title: '提示', offset: 't'},
               function (index) {
                  layer.close(index);

                  $.ajax({
                     type: 'PATCH',
                     url: '/admin/orders/change',
                     data: {id: id, attr: attr},
                     success: function (data) {
                        //判断有没有权限
                        if (data && data.status == 0) {
                           layer.msg(data.msg, {
                              icon: 5,
                              time: 2000, //2秒关闭（如果不配置，默认是3秒）
                              offset: 't'
                           });
                           return false;
                        } else {
                           _this.toggleClass('fa fa-check-circle fa fa-times-circle');
                        }
                     }
                  })
               });
            return false;
         });

         //订单时间
         lay('.order_time').each(function () {
            laydate.render({
               elem: this, //指定元素
               type: 'datetime',
               done: function (value) {
                  // console.log(value); //得到日期生成的值，如：2017-08-18
                  var info = {
                     id: this.elem.attr('data-id'),
                     name: this.elem.attr('name'),
                     val: value
                  };
                  edit_tract(info);
                  return false;
               }
            });
         });

         //跟妆日期
         lay('.hz_time').each(function () {
            laydate.render({
               elem: this, //指定元素
               type: 'datetime',
               done: function (value) {
                  // console.log(value); //得到日期生成的值，如：2017-08-18
                  var info = {
                     id: this.elem.attr('data-id'),
                     name: this.elem.attr('name'),
                     val: value
                  };
                  edit_tract(info);
                  return false;
               }
            });
         });

         //编辑订单编号，金额等信息
         $('.edit').change(function () {
            var info = {
               id: $(this).parents('tr').data('id'),
               val: $(this).val(),
               name: $(this).attr('name'),
            };

            edit_tract(info);
            return false;
         });

         //编辑套餐，化妆师信息
         $('.update').click(function () {
            var info = {
               id: $(this).parents('tr').data('id'),
               val: $(this).data('value'),
               name: $(this).data('name'),
            };
            edit_tract(info);
            return false;
         });

         //更新数据的公共方法
         function edit_tract(info) {
            layer.confirm('确认更新信息吗?',
               {icon: 3, title: '提示', offset: 't'},
               function (index) {
                  layer.close(index);

                  $.ajax({
                     url: "{{:url('orders/edit_order')}}",
                     data: info,
                     type: 'POST',
                     success: function (data) {
                        //判断有没有权限
                        if (data && data.status == 0) {
                           layer.msg(data.msg, {
                              icon: 5,
                              time: 2000, //2秒关闭（如果不配置，默认是3秒）
                              offset: 't'
                           });
                           return false;
                        } else {
                           window.location.reload();
                        }
                     }
                  });
               }
            )
         };

         //备注
         $('.beizhu').click(function () {
            var info = {
               id: $(this).parents('tr').data('id'),
               val: $(this).data('value')
            };
            //订单备注
            $('#mark1').val(info.id);
            $('#order_mark').val(info.val);
            //二次追踪详情
            $('#mark2').val(info.id);
            $('#mark').val(info.val);
         });
         //一次追踪详情
         $('.detailSubmit1').click(function () {
            var info = {
               id: $('#mark1').val(),
               name: $('#order_mark').attr('name'),
               val: $('#order_mark').val()
            };
            edit_tract(info);
            return false;
         });
         //二次追踪详情
         $('.detailSubmit2').click(function () {
            var info = {
               id: $('#mark2').val(),
               name: $('#mark').attr('name'),
               val: $('#mark').val()
            };
            edit_tract(info);
            return false;
         });

      })
   </script>

</block>



